#include "tool_common.h"

#include "cJSON.h"

static const char *TAG_TOOL = "TOOL";
/**
 * @brief 设置config.json的模式AP_DNS和AP_STA模式
 * 
 * @param type AP_DNS / AP_STA
 */
void set_config_module(config_esp_module type)
{
    char *file_content = NULL;
    size_t file_len = 0;

    // 读取config.json文件
    if (read_spiffs_file("/config.json", &file_content, &file_len) != ESP_OK) {
        return;
    }

    // 解析JSON
    cJSON *root = cJSON_Parse(file_content);
    if (root == NULL) {
        ESP_LOGE(TAG_TOOL, "Parse JSON failed");
        free(file_content);
        return;
    }

    // 获取module
    cJSON *module = cJSON_GetObjectItem(root, "module");
    if (module == NULL) {
        ESP_LOGE(TAG_TOOL, "Get module failed");
        cJSON_Delete(root);
        free(file_content);
        return;
    }

    if (cJSON_IsString(module) && (module->valuestring != NULL)) {
        switch (type) {
            case AP_DNS:
                cJSON_SetValuestring(module, "AP_DNS");
                break;
            case AP_STA:
                cJSON_SetValuestring(module, "AP_STA");
                break;
            default:
                break;
        }
    }

    // 将修改后的JSON写回文件
    char *modified_json = cJSON_Print(root);
    write_spiffs_file("/config.json", modified_json);

    // 清理
    cJSON_Delete(root);
    free(modified_json);
    free(file_content);
}
/**
 * @brief 获取Config.json的模式AP_DNS和AP_STA模式
 * 
 * @return config_esp_module 
 * @retval AP_DNS
 * @retval AP_STA
 */
config_esp_module get_config_module(void)
{
    char *file_content = NULL;
    size_t file_len = 0;
    config_esp_module val = AP_DNS;

    // 读取config.json文件
    if (read_spiffs_file("/config.json", &file_content, &file_len) != ESP_OK) {
        return ESP_FAIL;
    }

    // 解析JSON
    cJSON *root = cJSON_Parse(file_content);
    if (root == NULL) {
        ESP_LOGE(TAG_TOOL, "Parse JSON failed");
        free(file_content);
        return ESP_FAIL;
    }

    // 获取module
    cJSON *module = cJSON_GetObjectItem(root, "module");
    if (module == NULL) {
        ESP_LOGE(TAG_TOOL, "Get module failed");
        cJSON_Delete(root);
        free(file_content);
        return ESP_FAIL;
    }

    if (cJSON_IsString(module) && (module->valuestring != NULL)) {
        if (strcmp(module->valuestring, "AP_DNS") == 0) {
            val = AP_DNS;
        } else if (strcmp(module->valuestring, "AP_STA") == 0) {
            val = AP_STA;
        }
    }
    
    cJSON_Delete(root);
    free(file_content);

    return val;
}

/**
 * @brief 设置WIFI STA模式的SSID和PASSWD
 * 
 * @param ssid WIFI名称
 * @param passwd WIFI密码
 * @return esp_err_t 
 * @retval ESP_OK
 * @retval ESP_FAIL
 */
esp_err_t set_sta_ssid_passwid(char *ssid, char *passwd)
{
    char *file_content = NULL;
    size_t file_len = 0;

    // 读取config.json文件
    if (read_spiffs_file("/config.json", &file_content, &file_len) != ESP_OK) {
        return ESP_FAIL;
    }

    // 解析JSON
    cJSON *root = cJSON_Parse(file_content);
    if (root == NULL) {
        ESP_LOGE(TAG_TOOL, "Parse JSON failed");
        free(file_content);
        return ESP_FAIL;
    }

    // 获取sta_ssid
    cJSON *sta_ssid = cJSON_GetObjectItem(root, "SSID");
    if (sta_ssid == NULL) {
        ESP_LOGE(TAG_TOOL, "Get SSID failed");
        cJSON_Delete(root);
        free(file_content);
        return ESP_FAIL;
    }

    if (cJSON_IsString(sta_ssid) && (sta_ssid->valuestring != NULL)) {
        // 设置JSON新值
        cJSON_SetValuestring(sta_ssid, ssid);
    }

    // 获取sta_passwd
    cJSON *sta_passwd = cJSON_GetObjectItem(root, "password");
    if (sta_passwd == NULL) {
        ESP_LOGE(TAG_TOOL, "Get password failed");
        cJSON_Delete(root);
        free(file_content);
        return ESP_FAIL;
    }

    if (cJSON_IsString(sta_passwd) && (sta_passwd->valuestring != NULL)) {
        // 设置JSON新值
        cJSON_SetValuestring(sta_passwd, passwd);
    }

    // 将修改后的JSON写回文件
    char *modified_json = cJSON_Print(root);
    write_spiffs_file("/config.json", modified_json);

    // 清理
    cJSON_Delete(root);
    free(modified_json);
    free(file_content);

    return ESP_OK;
}

/**
 * @brief 获取WIFI STA模式的SSID和PASSWD
 * 
 * @param ssid WIFI名称
 * @param passwd WIFI密码
 * @return esp_err_t 
 * @retval ESP_OK
 * @retval ESP_FAIL
 */
esp_err_t get_sta_ssid_passwid(uint8_t *ssid, uint8_t *passwd)
{
    char *file_content = NULL;
    size_t file_len = 0;

    // 读取config.json文件
    if (read_spiffs_file("/config.json", &file_content, &file_len) != ESP_OK) {
        return ESP_FAIL;
    }

    // 解析JSON
    cJSON *root = cJSON_Parse(file_content);
    if (root == NULL) {
        ESP_LOGE(TAG_TOOL, "Parse JSON failed");
        free(file_content);
        return ESP_FAIL;
    }

    // 获取sta_ssid
    cJSON *sta_ssid = cJSON_GetObjectItem(root, "SSID");
    if (sta_ssid == NULL) {
        ESP_LOGE(TAG_TOOL, "Get SSID failed");
        cJSON_Delete(root);
        free(file_content);
        return ESP_FAIL;
    }

    if (cJSON_IsString(sta_ssid) && (sta_ssid->valuestring != NULL)) {
        memcpy(ssid, sta_ssid->valuestring, strlen(sta_ssid->valuestring));
    }

    // 获取sta_passwd
    cJSON *sta_passwd = cJSON_GetObjectItem(root, "password");
    if (sta_passwd == NULL) {
        ESP_LOGE(TAG_TOOL, "Get password failed");
        cJSON_Delete(root);
        free(file_content);
        return ESP_FAIL;
    }

    if (cJSON_IsString(sta_passwd) && (sta_passwd->valuestring != NULL)) {
        memcpy(passwd, sta_passwd->valuestring, strlen(sta_passwd->valuestring));
    }

    cJSON_Delete(root);
    free(file_content);

    return ESP_OK;
}

/**
 * @brief 获取扫描到的WIFI的数组、数量
 * 
 * @param number 
 * @param ap_info 
 * @param ap_count 
 * @return esp_err_t 
 */
esp_err_t get_wifi_ap_array(uint16_t *number, wifi_ap_record_t *ap_info, uint16_t *ap_count)
{
    // uint16_t number = 12;
    // wifi_ap_record_t ap_info[12];
    // uint16_t ap_count = 0;

    /* 开始扫描附近的WIFI */    
    esp_wifi_scan_start(NULL, true); 
    // ESP_LOGI(TAG_TOOL, "Max AP number ap_info can hold = %u", number);
    /* 获取上次扫描中找到的AP列表 */       
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(number, ap_info));
    /* 获取上次扫描中找到的AP数量 */    
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(ap_count));
    // ESP_LOGI(TAG_TOOL, "Total APs scanned = %u, actual AP number ap_info holds = %u", ap_count, number);
    // /* 下面是打印附件的WIFI信息 */
    // for (int i = 0; i < number; i++) {
    //     ESP_LOGI(TAG_TOOL, "SSID \t\t%s", ap_info[i].ssid);
    //     ESP_LOGI(TAG_TOOL, "RSSI \t\t%d", ap_info[i].rssi);
    // }

    return ESP_OK;
}